--http://tracking.logging.admicro.vn/_tracking.gif?fl=-1.-1.&je=0&sr=12801024&sc=24&hn=m.dantri.com.vn&p=%2Fc202%2Fs202-655053%2Fky-an-194-pho-hue-day-len-du-luan-ho-nghi-ve-the-luc-chong-lung.htm&r=http%3A%2F%2Fm.dantri.com.vn%2FDefault.htm&g=0

local biz_name = "_tracking.gif"
if test then
    ConfigRedisConnectors[biz_name]={
        host = test.host,
        port = test.port,
        queue = "k1"
    }
else
    ConfigRedisConnectors[biz_name]={
            --host = "192.168.2.28",
            --port = 6379,
            --host = "192.168.6.203",
			--host = "192.168.5.141",
			host = "192.168.5.201",
			port = 3579,  
            queue = "k1"
    }
end

local function _tracking()
return coroutine.create(function ()

-- constants
local REDIS_SEPERATOR = "|"
local NUM_SEMICOLON = 11
local DEF_LOCATION = "0"

local lpush_cmd_format = "lpush "..ConfigRedisConnectors[biz_name].queue.." %s "
local hget_cmd_format = "hget "..ConfigRedisConnectors[config.HGET_MOBILE_REDIS].queue.." %s "
local cookie_domain = config.http.setcookie.pc
local regions = region_table
local http_server_name = config.http.servername

-- headers
local local_config = config
local COOKIE_HEADER = local_config.COOKIE_HEADER
local USER_AGENT_HEADER = local_config.USER_AGENT_HEADER
local SET_COOKIE_HEADER = local_config.SET_COOKIE_HEADER
local REFERER_HEADER = local_config.REFERER_HEADER
local SERVER_HEADER = local_config.SERVER_HEADER
local http_server_name = local_config.http.servername
local PRAGMA_HEADER = local_config.PRAGMA_HEADER
local CACHE_CONTROL_HEADER = local_config.CACHE_CONTROL_HEADER
local EXPIRES_HEADER = local_config.EXPIRES_HEADER
local LAST_MODIFIED_HEADER = local_config.LAST_MODIFIED_HEADER
local CONTENT_TYPE_HEADER = local_config.CONTENT_TYPE_HEADER
--cookies
local REGION_COOKIE = local_config.REGION_COOKIE
local GUID_COOKIE = local_config.GUID_COOKIE
local CREATE_COOKIE = local_config.CREATE_COOKIE
local EXPIRE_TIME = local_config.EXPIRE_TIME
local OS_MOBILE_COOKIE = local_config.OS_MOBILE_COOKIE
local HGET_MOBILE_REDIS = local_config.HGET_MOBILE_REDIS
-- functions
local reply_pushHeader = reply.pushHeader
local reply_ok = reply.status_type.ok
local reply_setStatus = reply.setStatus
local reply_appendContent= reply.appendContent
local request_getHeadersKV = request.getHeadersKV
local request_getParamsKV = request.getParamsKV
local request_getIP = request.getIP
local os_time = os.time
local to_number = tonumber
local util_redis_cmd = util.redis_cmd
local util_c_inet_network = util.c_inet_network
local luredis_clear_buffer = luredis.clear_buffer
local luredis_concat_buffer = luredis.concat_buffer
local luredis_send_buffer = luredis.send_buffer
local luredis_send_cmd = luredis.send_cmd
local luredis_reply_string = luredis.reply_type.string
local url_decode = util.fast_url_decode
local l_pushCookieHeader = pushCookieHeader
local l_create_guid = create_guid
local l_parseCookies = parseCookies
local l_getMobileService = getMobileService
local coroutine_yield = coroutine.yield
local util_create_guid = util.create_guid

while true do
    -- table
    local headerTable = request_getHeadersKV();
    local user_agent = headerTable[USER_AGENT_HEADER]
    local referer = headerTable[REFERER_HEADER];
    -- cookie
    local cookieString = headerTable[COOKIE_HEADER];
    local cookieTable = l_parseCookies(cookieString);
    -- Get guid: guid
    local guid = cookieTable[GUID_COOKIE];
    local create_cookie = cookieTable[CREATE_COOKIE]
    local create_time = os_time();
    local expire_time = EXPIRE_TIME+create_time;
    local expire_time = EXPIRE_TIME+create_time;
    local ip = request_getIP()
	    local newguid = "0";
    -- guid
    if not guid then
        guid = util_create_guid(create_time, ip);
        if guid then
            create_cookie = create_time
            local expire_time = EXPIRE_TIME+create_time;
            l_pushCookieHeader(GUID_COOKIE, guid, expire_time, "/", cookie_domain, false)
            l_pushCookieHeader(CREATE_COOKIE, create_time, expire_time, "/", cookie_domain, false)
        else
            guid = "0"
        end
        newguid="1"
    end
    if not create_cookie then 
    	create_cookie = create_time
	    l_pushCookieHeader(CREATE_COOKIE, create_time, expire_time, "/", cookie_domain, false)
    end

    -- get region
    local region = cookieTable[REGION_COOKIE];
    if not region then -- ask redis for region
            --logic: query cache data on redis ==> if not --> query thrift
            --ip = "222.255.27.203"
            local ipnum = util_c_inet_network(ip)
            if ipnum then
                region = util_redis_cmd(HGET_MOBILE_REDIS,false,
                                        luredis_send_cmd,hget_cmd_format,ipnum);
                -- not on redis ==> query thrift
                if type(region) ~= "string" then
                        region = thrift_isIpvn()
                end
            end
            if region then
                l_pushCookieHeader(REGION_COOKIE, region, expire_time, "/", cookie_domain, false)
            else
                region = DEF_LOCATION
            end
    end

    local paraTable = request_getParamsKV("&=");
    ---------
    luredis_clear_buffer()
    luredis_concat_buffer(REDIS_SEPERATOR,
        create_time, create_cookie, user_agent, ip,
        paraTable["hn"], url_decode(paraTable["p"]), paraTable["r"],
        guid,paraTable["fl"], paraTable["je"], paraTable["sr"],
        paraTable["sc"], region, paraTable["g"])
    util_redis_cmd(biz_name,true,luredis_send_buffer,lpush_cmd_format);
	reply_setStatus(reply_ok)
    -- reply --
    reply_pushHeader(CONTENT_TYPE_HEADER, "application/javascript")
    reply_pushHeader(SERVER_HEADER,http_server_name)
    local rid = to_number(region)
    if not rid or rid == 6 or rid < 0 or rid > 68 then
        rid = "0"
    else
        rid = regions[rid]
    end
    reply_appendContent("function getCookie(a){if(document.cookie.length>0&&(c_start=document.cookie.indexOf(a+\"=\"),c_start!=-1)){c_start=c_start+a.length+1;c_end=document.cookie.indexOf(\";\",c_start);if(c_end==-1)c_end=document.cookie.length;return unescape(document.cookie.substring(c_start,c_end))}return\"\"}function setCookie(a,d,c){var b=new Date;b.setDate(b.getDate()+c);document.cookie=a+\"=\"+escape(d)+(c==null?\"\":\";path=\\/;expires=\"+b.toUTCString())}var _ADSLocation=getCookie(\"__R\"),_ADSCity=getCookie(\"__RC\");"
        ,"parseInt(_ADSCity)!="
        , region, " && setCookie('__RC'," , region, ", null);parseInt(_ADSLocation) != "
        , rid, " && setCookie('__R', " , rid, ", null);")
    coroutine_yield() -- norma
end  -- of while
end) -- of anonymous function
end  -- of tkm()

creators[biz_name] = function()
        f[biz_name] = _tracking()
end
